package com.yan.lesson01;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class TcpClientDemo02 {
    public TcpClientDemo02() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
        //1. 创建一个Socket对象，构造方法中绑定服务器的IP地址和端口号
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8888);
        //2. 创建一个输出流
        OutputStream os = socket.getOutputStream();
        //3. 读取文件
        FileInputStream fis = new FileInputStream(new File("1.png"));
        //4. 写出文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        //5. 关闭资源
        fis.close();
        os.close();
        socket.close();
    }
}
